package com.wh.message.sms.service.impl;

import com.wh.common.util.Result;
import com.wh.message.sms.in.SendSmsIn;
import com.wh.message.sms.service.AliyunMassageService;
import com.wh.message.sms.utils.SmsAliyunConfig;
import com.wh.message.sms.utils.SmsConfig;
import com.wh.message.utils.ValidatorFactoryUtil;
import com.wh.msg.ResultCode;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.util.HashMap;
import java.util.Map;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 阿里云短信发送消费者
 * @author 谢泽鹏
 * @version V1.0
 * @Package com.wh.message.mongo.service.impl
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
public class AliyunMessageServiceImpl implements AliyunMassageService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 短信配置对象
     */
    private SmsAliyunConfig smsAliyunConfig;

    /**
     * 设置短信配置
     * @param smsAliyunConfig
     */
    public void setSmsAliyunConfig(SmsAliyunConfig smsAliyunConfig) {
        this.smsAliyunConfig = smsAliyunConfig;
    }

    @Override
    public Result sendSms(SendSmsIn sendSmsIn) {
        logger.info("sendSms阿里短信-->>开始");
        logger.info("sendSms阿里短信-->>sendSmsIn = {}", JSONObject.fromObject(sendSmsIn).toString());
        logger.info("sendSms阿里短信-->>smsConfig = {}", JSONObject.fromObject(smsAliyunConfig).toString());
        Result result = new Result();
        try {
            result = ValidatorFactoryUtil.validate(sendSmsIn);
            if(!ResultCode.RES_OK.equals(result.getResCode())){
                logger.info("sendSms阿里短信-->>参数校验不通过-->>result = {}", result);
                return result;
            }

            //可自助调整超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", smsAliyunConfig.getDefaultConnectTimeout());
            System.setProperty("sun.net.client.defaultReadTimeout", smsAliyunConfig.getDefaultReadTimeout());
            //初始化acsClient
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsAliyunConfig.getAccessKeyId(), smsAliyunConfig.getAccessKeySecret());
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", smsAliyunConfig.getProduct(), smsAliyunConfig.getDomain());
            IAcsClient acsClient = new DefaultAcsClient(profile);
            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //发送手机号
            request.setPhoneNumbers(sendSmsIn.getPhone());
            //短信签名
            request.setSignName(smsAliyunConfig.getSignName());
            //短信模板
            request.setTemplateCode(smsAliyunConfig.getTemplateCode());
            //可选:模板中的变量替换JSON串,如模板内容为""时,此处的值为
            request.setTemplateParam(sendSmsIn.getContent());
            //短信回执(暂无使用)
            request.setOutId(smsAliyunConfig.getOutId());
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            //短信判定
            if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                result.setResCode(ResultCode.RES_200);
                result.setResMsg("短信发送成功!");
                result.setResult(new JSONObject());
                logger.info("sendSms阿里短信->>成功, code : " + sendSmsResponse.getCode() + ", message : " + sendSmsResponse.getMessage());
            }else{
                result.setResCode(ResultCode.RES_NO);
                result.setResMsg("短信发送失败!");
                result.setResult(new JSONObject());
                logger.info("sendSms阿里短信->>失败, code : " + sendSmsResponse.getCode() + ", message : " + sendSmsResponse.getMessage());
            }
            logger.info("sendSms阿里短信-->>sendSms阿里短信,成功");
        }catch (Exception e){
            logger.error("sendSms阿里短信-->>sendSms阿里短信-->>出现异常msg={}",e.getMessage());
            e.printStackTrace();
            result.setResCode(ResultCode.RES_ERR);
            result.setResult(e.getMessage());
        }
            logger.info("sendSms阿里短信-->>结束");
            return result;
    }




}
